import {type ClassValue, clsx} from 'clsx';
import {twMerge} from 'tailwind-merge';

export function cn(...inputs: ClassValue[]) {
    return twMerge(clsx(inputs));
}

export function generateId(): string {
    return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}

export function formatTime(timestamp: number): string {
    const date = new Date(timestamp);
    const now = new Date();
    const diffMs = now.getTime() - date.getTime();
    const diffMins = Math.floor(diffMs / 60000);
    const diffHours = Math.floor(diffMins / 60);
    const diffDays = Math.floor(diffHours / 24);

    if (diffMins < 1) return 'just now';
    if (diffMins < 60) return `${diffMins}m ago`;
    if (diffHours < 24) return `${diffHours}h ago`;
    if (diffDays < 7) return `${diffDays}d ago`;

    return date.toLocaleDateString();
}

export function copyToClipboard(text: string): Promise<boolean> {
    return navigator.clipboard
        .writeText(text)
        .then(() => true)
        .catch(() => false);
}

export function parseMarkdownCode(markdown: string): Array<{
    type: 'text' | 'code';
    content: string;
    language?: string;
}> {
    const codeBlockRegex = /```(\w*)\n([\s\S]*?)\n```/g;
    const parts: Array<{
        type: 'text' | 'code';
        content: string;
        language?: string;
    }> = [];

    let lastIndex = 0;
    let match;

    while ((match = codeBlockRegex.exec(markdown)) !== null) {
        // Add text before code block
        if (match.index > lastIndex) {
            parts.push({
                type: 'text',
                content: markdown.substring(lastIndex, match.index),
            });
        }

        // Add code block
        parts.push({
            type: 'code',
            language: match[1] || 'plaintext',
            content: match[2],
        });

        lastIndex = match.index + match[0].length;
    }

    // Add remaining text
    if (lastIndex < markdown.length) {
        parts.push({
            type: 'text',
            content: markdown.substring(lastIndex),
        });
    }

    return parts.length > 0 ? parts : [{type: 'text', content: markdown}];
}
